package com.hospital.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.model.Department;
import com.hospital.util.DbUtil;
@WebServlet("/manage/MedicineSaleServlet")
public class MedicineSaleServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {//药品的售卖的Servlet
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			DbUtil dbutil = new DbUtil();
			String ypmc = request.getParameter("ypmc").trim();
			String ypsl = request.getParameter("ypsl").trim();
			int j = 0;
			j = dbutil.getIf(ypmc);
			int k = 0;
			k = dbutil.getSale(ypmc);
			if(Integer.parseInt(ypsl) > (j-k)){//首先要确定药品量是否充足
				request.setAttribute("message", "该药品共进货"+j+",累计出售"+k+",剩余"+(j-k)+",您需要出售的数量为"+ypsl+"，操作失败！");
				request.getRequestDispatcher("/manage/ypkaiyao_info_add.jsp").forward(request, response);
			}else{
				String ypsalemoney = null;
				String sqlmoeny = "select * from t_medicinename where id = "+ ypmc;
				List<Map<String, Object>> listmoney = DbUtil.find(sqlmoeny);
				Map<String, Object> mapmoeny = null;
				//从药品的名字表中得到药品的销售价格
				for (int i = 0; i < listmoney.size(); i++) {
					mapmoeny = (Map<String, Object>)listmoney.get(i);
					ypsalemoney = (String)mapmoeny.get("money");
				}
				double ypsj = Double.parseDouble(ypsalemoney);
				String sumjg = String.valueOf(Integer.parseInt(ypsl) * ypsj);
				String sql = "insert into t_sale(ypmc,ypsj,ypsl,sumjg,xsrq) values('"
						+ ypmc +"', '" + ypsj + "', '" + ypsl + "','" + sumjg + "', '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sql);
				String sqlsy = "update t_stroage set ypsysl = '" + (j-k-Integer.parseInt(ypsl)) + "' where ypmc = " + ypmc;
				DbUtil.add_modify(sqlsy);
				request.getRequestDispatcher("/manage/ypkaiyao_info_manage.jsp").forward(request, response);
			}
		} else if ("del".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			DbUtil.del("t_sale", id);
			request.getRequestDispatcher("/manage/ypkaiyao_info_manage.jsp").forward(request, response);
		} else if ("modify".equals(method)) {
			int id = Integer.parseInt(request.getParameter("id"));
			String ypmc = request.getParameter("ypmc");
			String ypsl = request.getParameter("ypsl");
			String ypsalemoney = null;
			String sqlmoeny = "select * from t_medicinename where id = "+ ypmc;	
			Map<String, Object> mapmoeny  = (Map<String, Object>)DbUtil.find(sqlmoeny).get(0);
			//从药品的名字表中得到药品的销售价格
			ypsalemoney = (String)mapmoeny.get("money");
			double ypsj = Double.parseDouble(ypsalemoney);
			String sumjg = String.valueOf(Integer.parseInt(ypsl) * ypsj);
			String sql = "update t_sale set ypsl = '" + ypsl + "', sumjg = '" + sumjg + "' where id = " + id;
			DbUtil.add_modify(sql);
			request.getRequestDispatcher("/manage/ypkaiyao_info_manage.jsp").forward(request, response);
		}	
	}
}